package com.xmim.core.container.handler;

import com.xmim.core.constants.MessageConstants;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractHandlerManager<N, H> implements HandlerManager<N, H> {
    protected Map<N, H> handlers = new HashMap<>();

    @Override
    public AbstractHandlerManager<N, H> addHandler(N type, H handler) {
        if (!this.checkHandler(handler.getClass())) {
            throw new IllegalArgumentException(MessageConstants.INVALID_HANDLER);
        }
        this.handlers.put(type, handler);
        return this;
    }


    @Override
    public Enumeration<Map.Entry<N, H>> enumerations() {
        return Collections.enumeration(handlers.entrySet());
    }

    @Override
    public H getHandler(N type) {
        return handlers.get(type);
    }

}
